#! /bin/sh

source /etc/rc.d/common

PACEMAKERD="/usr/sbin/pacemakerd"
PACEMAKERD_LOCK="/var/lock/pacemakerd"
DFLT_OPTS="> /dev/null 2>&1"

check_args ${@}

SCRIPT="$(/usr/bin/basename ${0})"
if check_opts_set ${SCRIPT}; then
    USER_OPTS="$(get_rc_opts ${SCRIPT})"
    if [ ${?} -ne 0 ]; then
        /bin/echo ${USER_OPTS}
        exit 1
    fi
else
    USER_OPTS="${DFLT_OPTS}"
fi

start() {
    if check_enabled "rc.corosync"; then
        if ! /etc/rc.d/rc.corosync status; then
            /bin/echo "Corosync is not running, skipping Pacemaker start!"
            exit 1
        fi
    fi
    /bin/echo "Starting pacemakerd..."
    /bin/sh -c "/usr/bin/nohup ${PACEMAKERD} ${USER_OPTS} &"
    /bin/touch ${PACEMAKERD_LOCK}
    /bin/sleep 1
}

stop() {
    /bin/echo "Stopping pacemakerd..."
    /bin/kill -TERM $(/bin/pidof ${PACEMAKERD}) || exit 1
    wait_for_stop ${PACEMAKERD} && /bin/rm -f ${PACEMAKERD_LOCK}
}

status() {
    /bin/pidof ${PACEMAKERD} > /dev/null 2>&1
    exit ${?}
}

# Perform specified action
${1}
